package com.heima.login.controller.v1;

import com.heima.login.apis.LoginControllerApi;
import com.heima.login.service.ApUserLoginService;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.user.pojos.ApUser;
import com.heima.model.user.pojos.TransactionList;
import com.heima.utils.common.IpUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.Swagger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.core.*;
import com.fasterxml.jackson.databind.*;

import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.*;
import java.io.IOException;
import java.lang.reflect.Array;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

import java.util.Random;


@Api(value = "商户平台-应用管理", tags = "商户平台-应用相关")
@RestController
@RequestMapping("/vue-element-admin")
public class LoginController implements LoginControllerApi {

    @Autowired
    private ApUserLoginService apUserLoginService;

    @Autowired
    private HttpServletRequest request;

    @Override
    @GetMapping("/user/info")
    public Map getUserInfo(@RequestParam Map<String, String> params) {

        //return apUserLoginService.loginAuth(user);

        Map<String,Object> userinfo= new HashMap();
        //@PathVariable("id") Long id   @RequestParam 和 @PathVariable 注解是用于从 request 中接收请求的，两个都可以接收参数，关键点不同的是@RequestParam 是从 request 里面拿取值，而 @PathVariable 是从一个URI模板里面来填充
        String[] roles = new String[1];
        roles[0] = "admin";
        userinfo.put("introduction","I am a super administrator");
        userinfo.put("avatar","https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif");
        userinfo.put("name","Super Admin");
        userinfo.put("roles",roles);


        Map<String,Object>token= new HashMap();

        token.put("code",20000);
        token.put("data",userinfo);
        return token;

    }



/*    使用于在方法上，表示一个http请求的操作
    @ApiOperation(value = “接口说明”, httpMethod = “接口请求方式”, response = “接口返回参数类型”, notes = “接口发布说明”)
    value：对该操作进行简单的描述，尽量控制在120字符以内。
    notes：对操作的详细描述。
    httpMethod：指定操作使用的HTTP方法类型，可选值 GET、HEAD、POST、PUT、DELETE、OPTIONS、PATCH。
    tags：用来给操作打标签，
    Swagger UI 将在操作列表下面展示 tag 列表，每个 tag 下面展示拥有该 tag 的操作列表。（就是分组）*/
    @Override
    @ApiOperation(value="根据id查询商户信息")
    @PostMapping("/user/login")
    public Map userLogin(@RequestParam Map<String, String> params){

        // 限制 调用的
        String username = params.get("username");
        String password = params.get("password");
        String vId = params.get("vId");
        String ips= IpUtils.getIpAddress(this.request);

        Map<String,Object>token= new HashMap();
        //@PathVariable("id") Long id   @RequestParam 和 @PathVariable 注解是用于从 request 中接收请求的，两个都可以接收参数，关键点不同的是@RequestParam 是从 request 里面拿取值，而 @PathVariable 是从一个URI模板里面来填充
        Map<String,String>admin= new HashMap();
        admin.put("token","admin-token");

        token.put("code",20000);
        token.put("data",admin);

//        Map<String,String>editor= new HashMap();
//        editor.put("token","editor-token");
//        token.put("editor",editor);
         return token;
    }


    // @RequestParam("aaa") String aaa
    @Override
    @GetMapping("/routes")
    public Map getRoutes(){

        Map<String,Object>mp= new HashMap();

       // String json="{\"path\":\"permission\",\"component\":\"layout//Layout\",\"redirect\":\"//permission//index\",\"alwaysShow\": true,\"meta\": {\"title\": \"permission\",\"icon\": \"lock\",\"roles\": [\"admin\", \"editor\"]},\"children\": [{\"path\": \"page\",\"component\": \"views//permission//page\",\"name\": \"PagePermission\",\"meta\": \"title\": \"pagePermission\",\"roles\": [\"admin\"]}},{\"path\": \"directive\",\"component\": \"views//permission//directive\",\"name\": \"DirectivePermission\",\"meta\": {\"title\": \"directivePermission\"}},{ \"path\": \"external-link\", \"component\": \"layout//Layout\", \"children\": [{\"path\": \"https://github.com/PanJiaChen/vue-element-admin\",\"meta\": {\"title\": \"externalLink\",\"icon\": \"link\" } }]},{ \"path\": '*', \"redirect\": \"//404\", \"hidden\": true }";



        String json = "{\"id\":1,\"name\":\"Lokesh Gupta\",\"age\":34,\"location\":false}";
        //ObjectMapper objectMapper = new ObjectMapper();
        //Map<String, Object> map = objectMapper.readValue(json, HashMap.class);

        HashMap<String, Object> map = new HashMap<String, Object>();
        ObjectMapper mapper = new ObjectMapper();
        mapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_CONTROL_CHARS, true);
        mapper.configure(JsonGenerator.Feature.ESCAPE_NON_ASCII, false);

        try
        {
            //Convert Map to JSON
            map = mapper.readValue(json, new TypeReference<Map<String, Object>>(){});
            mp.put("code",20000);
            mp.put("data",map);

        } catch (JsonMappingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return mp;



    }

    @Override
    @GetMapping("/transaction/list")
    public Map getTransList(){


        //@PathVariable("id") Long id   @RequestParam 和 @PathVariable 注解是用于从 request 中接收请求的，两个都可以接收参数，关键点不同的是@RequestParam 是从 request 里面拿取值，而 @PathVariable 是从一个URI模板里面来填充
        Map<String,Object>mp= new HashMap();

        Map<String,Object>items= new HashMap();
        items.put("total",20);

        TransactionList[] tralist = new TransactionList[10];
        for(int i=0; i< tralist.length; i++) {
            Random random = new Random();
            long timestamp = random.nextLong() % System.currentTimeMillis();
            String st = "success";
            if(i/2==0){
                st = "pending";
            }
            float min = 1f;
            float max = 10f;
            float floatBounded = min + new Random().nextFloat() * (max - min);
            tralist[i]= new TransactionList();

            tralist[i].setOrder_no(this.generateGuid());
            tralist[i].setPrice(Float.valueOf(floatBounded));
            tralist[i].setTimestamp(Long.valueOf(timestamp));
            tralist[i].setUsername("username");
            tralist[i].setStatus(st);
        }

        items.put("items",tralist);
        mp.put("code",20000);
        mp.put("data",items);

        return mp;

    }

    public String generateGuid() {
        UUID uuid = UUID.randomUUID();
        return uuid.toString();
    }


    @GetMapping("/vue-element-admin/roles")
    public Map getRoles(){
        Map<String,Object>AA= new HashMap();
        //@PathVariable("id") Long id   @RequestParam 和 @PathVariable 注解是用于从 request 中接收请求的，两个都可以接收参数，关键点不同的是@RequestParam 是从 request 里面拿取值，而 @PathVariable 是从一个URI模板里面来填充
        AA.put("aaa",111);
        AA.put("博客地址", "http://blog.itcodai.com");
        AA.put("CSDN地址", null);
        AA.put("粉丝数量", 4153);

        return AA;
    }


}
